<template>
    <div class="app-container">
        <el-row :gutter="10">
            <el-col v-if="isShowLeftPatientInfo" :span="8">
                <nurse-patient-bed-list ref="nursePatientBedListRef"
                                        @patient-selection-change="patientSelectionChange"/>
            </el-col>
            <el-col :span="isShowLeftPatientInfo ? 16:24">
                <div class="search-container">
                    <el-form ref="queryParamsRef" :inline="true" :model="queryParams" label-width="100px">
                        <el-form-item label="长/临:" prop="repeatIncaditor">
                            <el-radio-group v-model="queryParams.repeatIncaditor" @change="orderListSelect">
                                <template v-for="(tag, index) in dictDataMap['order_type']">
                                    <el-radio-button :label="tag.dictLabel" :value="tag.dictValue"/>
                                </template>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item label="执行单类型:" prop="classifyValue">
                            <el-radio-group v-model="queryParams.classifyValue" @change="classifyValueChange">
                                <template v-for="(tag, index) in dictDataMap['orders_way_classify']">
                                    <el-radio-button :label="tag.dictLabel" :value="tag.dictValue"/>
                                </template>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item label="执行时间:" prop="executionTime">
                            <el-date-picker v-model="queryParams.executionTime"
                                            style="width: 100%"
                                            type="date"
                                            value-format="YYYY-MM-DD"/>
                        </el-form-item>
                        <el-form-item>
                            <query-form-button @handleQuery="orderListSelect" @resetQuery="resetQuery"/>
                            <el-button type="success" @click="classificationExecutionFormPrint">
                                <template #icon>
                                    <i-ep-printer/>
                                </template>
                                打印
                            </el-button>
                        </el-form-item>
                    </el-form>
                </div>
                <el-table v-adaptive="350" :border="true" :data="orderList" :header-cell-style="$headerCellStyle"
                          :stripe="true" highlight-current-row @select-all="selectAll">
                    <el-table-column :align="'center'" type="selection" width="40">
                        <template #default="scope">
                            <el-checkbox v-model="scope.row.checkOk"
                                         :disabled="!(scope.row.groupNumberStyle === '┌' || scope.row.groupNumberStyle === '')"
                                         @change="checkboxChange($event, scope.row)"/>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'left'" :header-align="'center'" :show-overflow-tooltip="true"
                                     label="医嘱内容"
                                     prop="orderText">
                        <template #default="scope">
                            <span
                                style="color: #000000;font-size: large;">{{
                                    scope.row.groupNumberStyle != '' ? scope.row.groupNumberStyle : '&nbsp;&nbsp;&nbsp;'
                                }}</span>
                            <span style="margin-left: 5px">{{ scope.row.orderText }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="长/临" prop="repeatIncaditor" width="60">
                        <template #default="scope">
                            <dict-data-tag :options="dictDataMap['order_type']"
                                           :value="scope.row.repeatIncaditor"/>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="是否计价" prop="billingAttr" width="90">
                        <template #default="scope">
                            <dict-data-tag :options="dictDataMap['billing_attr']"
                                           :value="scope.row.billingAttr"/>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'"
                                     :label="queryParams.classifyValue==='1' || queryParams.classifyValue==='2' || queryParams.classifyValue==='3' ? '药品规格' :'项目规格'"
                                     prop="packageSpec" width="130"/>
                    <el-table-column :align="'center'" label="途径" prop="channelName" width="120"/>
                    <el-table-column :align="'center'" label="频次" prop="performFreqName" width="110"/>
                    <el-table-column
                        v-if="queryParams.classifyValue==='1' || queryParams.classifyValue==='2' || queryParams.classifyValue==='3'"
                        :align="'center'"
                        label="单次用量" prop="numSingle" width="100">
                        <template #default="scope">
                            <span style="margin-left: -15px"> {{ parseFloat(scope.row.numSingle).toFixed(2) }}<dict-data-tag
                                :options="dictDataMap['spec_unit']" :value="scope.row.minUnits"
                                style="margin: -23px -20px 0px 35px"/></span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        v-if="queryParams.classifyValue==='1' || queryParams.classifyValue==='2' || queryParams.classifyValue==='3'"
                        :align="'center'"
                        label="单次剂量" prop="singleDosage" width="120">
                        <template #default="scope">
                            <span style="margin-left: -15px"> {{ parseFloat(scope.row.singleDosage).toFixed(2) }}<dict-data-tag
                                :options="dictDataMap['dose_unit']" :value="scope.row.doseUnits"
                                style="margin: -23px -20px 0px 35px"/></span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        v-if="queryParams.classifyValue==='1' || queryParams.classifyValue==='2' || queryParams.classifyValue==='3'"
                        :align="'center'" label="滴速"
                        prop="dripSpeed"
                        width="120">
                        <template #default="scope">
                            <dict-data-tag :options="dictDataMap['sys_drop_speed']"
                                           :value="scope.row.dripSpeed"/>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="医嘱说明" prop="remarks" width="120">
                        <template #default="scope">
                            {{ scope.row.remarks }}
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
    </div>
</template>

<script lang="ts" setup>
import {useDictStoreHook} from "@/store/modules/dict.ts";
import {findOrdersBottleLabel} from "@/api/orders/orders.ts";
import {useUserStoreHook} from "@/store/modules/user.ts";

const dictStore = useDictStoreHook();
let dictDataMap = ref(dictStore.isTypeGetData('billing_attr', 'orders_way_classify', 'order_type', 'sys_drop_speed',
    'spec_unit', 'dose_unit'))
const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;
// orders id 集合
const ids = ref<any>([])
const {proxy} = getCurrentInstance() as any;
const nursePatientBedListRef = ref()
const isShowLeftPatientInfo = ref<boolean>(true)
const orderList = ref<any>([])
const queryParams = ref<any>({
    visitIdArray: [],
    classifyValue: '3',
    repeatIncaditor: "0",
    executionTime: proxy.$parseTime(new Date(), "{y}-{m}-{d} ")
})


function handleQuery() {
    nextTick(() => {
        nursePatientBedListRef.value.handleQuery()
    })
}

/**
 * 执行单打印
 */
function classificationExecutionFormPrint() {
  debugger
    const classifyValueThis = queryParams.value.classifyValue
    const checkList = orderList.value.filter((item: any) => item.checkOk)
    if (checkList.length <= 0) {
        return ElMessage.error("请勾选需要打印的医嘱")
    }
    let ids = ""
    let visitIds = ""
    checkList.forEach((item: any) => {
        ids += item.id + ","
        visitIds += item.visitId + ","
    })
    let url = "/business/orders/classificationExecutionFormPrint";
    const formData = new FormData()
    formData.append("repeatIncaditor", queryParams.value.repeatIncaditor)
    formData.append("classifyValueThis", queryParams.value.classifyValue)
    formData.append("executionTime", queryParams.value.executionTime)
    formData.append("visitIdsStr", visitIds)
    formData.append("ordersIdListStr", ids)
    formData.append("orgId", userInfo.orgId)
  // pdf打印 、打印预览
  proxy.$printPdfPublic(url, formData).then(res => {
    if (res) {
      // objectUrl.value = URL.createObjectURL(res)
    } else {
      console.error('执行单打印失败')
    }
  })
}

/**
 * 分类执行单 点击事件
 * @param value
 */
function classifyValueChange(value: string) {
    queryParams.value.classifyValue = value
    orderListSelect()
}

/**
 * 查询执行单信息
 */
function orderListSelect() {
    if (queryParams.value.visitIdArray.length <= 0) {
        orderList.value = []
        return ElMessage.error("请勾选左侧患者信息后，再操作")
    }
    findOrdersBottleLabel(proxy.$addDateRange(queryParams.value, null)).then((res: resType) => {
        orderList.value = res.data
        ids.value = orderList.value
        selectAll()
    })
}

/**
 * 重置
 */
function resetQuery() {
    queryParams.value.visitIdArray = []
    queryParams.value.classifyValue = '3'
    queryParams.value.repeatIncaditor = 0
    queryParams.value.executionTime = proxy.$parseTime(new Date(), "{y}-{m}-{d} ")
}

/**
 * 患者床位列表勾选事件
 */
function patientSelectionChange(visitIds: any) {
    queryParams.value.visitIdArray = visitIds
    orderListSelect()
}

function empty() {

}

/**
 * 瓶签明细全选事件 全选/反选
 */
function selectAll() {
    if (ids.value.length === orderList.value.length) {
        orderList.value.forEach((order: any) => {
            order.checkOk = false
        })
        ids.value = []
    } else {
        orderList.value.forEach((order: any) => {
            order.checkOk = true
        })
        ids.value = orderList.value.map((item: any) => item.detailId);
    }
}

// 行选中 默认按照组号相同的勾选或者取消勾选
function checkboxChange(e: boolean, detail: any) {
    if (e) {
        orderList.value.forEach((order: any) => {
            if (detail.groupNumber != null && order.groupNumber === detail.groupNumber && order.visitId == detail.visitId) {
                order.checkOk = true
            }
        })
    } else {
        orderList.value.forEach((order: any) => {
            if (detail.groupNumber != null && order.groupNumber === detail.groupNumber && order.visitId == detail.visitId) {
                order.checkOk = false
            }
        })
    }

}

defineExpose({
    handleQuery,
    empty
})
</script>


<style scoped>

</style>
